﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using PCMSofCC;
using PCMSofCC.BLL;

namespace PCMSofCC.Admin
{
    public partial class ModifySite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GoogleMapForASPNet1.PushpinMoved += new GoogleMapForASPNet.PushpinMovedHandler(OnPushpinMoved);
            if (!IsPostBack)
            {
                GoogleMapForASPNet1.GoogleMapObject.APIKey = ConfigurationManager.AppSettings["GoogleAPIKey"];
                GoogleMapForASPNet1.GoogleMapObject.APIVersion = "2";
                GoogleMapForASPNet1.GoogleMapObject.Width = "590px";
                GoogleMapForASPNet1.GoogleMapObject.Height = "489px";
                GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 12;
                GoogleMapForASPNet1.GoogleMapObject.AutomaticBoundaryAndZoom = false;
                GooglePoint GP = new GooglePoint("1", 30.59, 114.30);
                GoogleMapForASPNet1.GoogleMapObject.CenterPoint = GP;
                GP.Draggable = true;
                GoogleMapForASPNet1.GoogleMapObject.Points.Add(GP);
            }
        }

        void OnPushpinMoved(string pID)
        {
            GooglePoint GP = GoogleMapForASPNet1.GoogleMapObject.Points["1"];
            lblPosition.Text = GP.Latitude.ToString("f4") + ", " + GP.Longitude.ToString("f4");
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            GooglePoint GP = GoogleMapForASPNet1.GoogleMapObject.Points["1"];
            Sites.UpdateSite(int.Parse(SiteList.SelectedValue),
                x =>
                {
                    x.Name = SiteName.Text;
                    x.Number = SiteNumber.Text;
                    x.Address = SiteAddress.Text;
                    x.Latitude = GP.Latitude;
                    x.Longitude = GP.Longitude;
                    x.CompanyId = int.Parse(SiteCompany.SelectedValue);
                    x.IsUsed = cbxIsUsed.Checked ? 1 : 0;
                    return x;
                });

        }

        protected void SiteList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Site site = Sites.GetSite(int.Parse(SiteList.SelectedValue));
            SiteName.Text = site.Name;
            SiteNumber.Text = site.Number;
            SiteAddress.Text = site.Address;
            GooglePoint GP = GoogleMapForASPNet1.GoogleMapObject.Points["1"];
            GP.Latitude = site.Latitude;
            GP.Longitude = site.Longitude;
            lblPosition.Text = GP.Latitude.ToString("f4") + ", " + GP.Longitude.ToString("f4");
            MapRecenter(GoogleMapForASPNet1.GoogleMapObject.Points["1"]);
            cbxIsUsed.Checked = (site.IsUsed == 1);
            SiteCompany.SelectedValue = site.CompanyId.ToString();
        }

        private void MapRecenter(GooglePoint point)
        {
            GoogleMapForASPNet1.GoogleMapObject.CenterPoint = point;
            GoogleMapForASPNet1.GoogleMapObject.RecenterMap = true;
        }
    }
}
